package com.zyj.myapplication.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zyj.myapplication.Activity.AboutYueJianActivity;
import com.zyj.myapplication.Activity.LoginActivity;
import com.zyj.myapplication.Activity.UserCollectActivity;
import com.zyj.myapplication.Activity.UserEditActivity;
import com.zyj.myapplication.Activity.UserOrderActivity;
import com.zyj.myapplication.Activity.UserTieziActivity;
import com.zyj.myapplication.Javabean.User;
import com.zyj.myapplication.Javabean.UserInfo;
import com.zyj.myapplication.Model.Constant;
import com.zyj.myapplication.Model.UserModel;
import com.zyj.myapplication.R;

import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class MyFragment extends Fragment {
    private RecyclerView recyclerView;
    private LinearLayout My_tiezi, My_collect, My_order, My_about;
    private ImageView My_photo;
    private TextView My_name;
    private Button my_edit, user_exit;
    private int userid;
    private User userinfo;
    private UserInfo userInfo;
    private Context mContext;
    private View view = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my, container, false);
        initViews();//初始化
        initEvents();//初始化事件
        showData();
        return view;
    }


    private void initViews() {
        My_name = (TextView) view.findViewById(R.id.My_name);
        My_photo = (ImageView) view.findViewById(R.id.My_photo);
        My_tiezi = (LinearLayout) view.findViewById(R.id.My_tiezi);
        My_collect = (LinearLayout) view.findViewById(R.id.My_collect);
        My_order = (LinearLayout) view.findViewById(R.id.My_order);
        My_about = (LinearLayout) view.findViewById(R.id.My_about);
        my_edit = (Button) view.findViewById(R.id.my_edit);
        user_exit = (Button) view.findViewById(R.id.user_exit);
        userid = getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id", 0);
    }

    private void initEvents() {
        user_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                //   Toast.makeText(mContext, "退出登录成功", Toast.LENGTH_SHORT).show();
            }
        });

        my_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserEditActivity.class);
                startActivity(intent);
            }
        });
        My_tiezi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserTieziActivity.class);
                intent.putExtra("user_name", userinfo.getUser_name());
           //     intent.putExtra("user_photo", (String) userinfo.getPhoto());
                startActivity(intent);
            }
        });
        My_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserCollectActivity.class);
                startActivity(intent);
            }
        });
        My_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserOrderActivity.class);
                intent.putExtra("user_name", userinfo.getUser_name());
                intent.putExtra("user_photo", (String) userinfo.getPhoto());
                startActivity(intent);
            }
        });
        My_about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AboutYueJianActivity.class);
                startActivity(intent);
            }
        });
    }

    private void showData() {
        UserModel userModel = new UserModel();
        Call<User> call = userModel.getUserinfo(userid);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                userinfo = response.body();
                My_name.setText(userinfo.getUser_name());
                Picasso.with(mContext)
                        .load(Constant.PIC_URL + userinfo.getPhoto())
                        .placeholder(R.drawable.tx)
                        .noFade()
                        .into(My_photo);
               /* if (!"".equals(userinfo.getPhoto())) {
                    Picasso.with(mContext).load(Constant.PIC_URL + userinfo.getPhoto()).into(My_photo);
                } else {
                    Picasso.with(getContext()).load(R.drawable.tx).into(My_photo);
                }*/
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        });
    }
}
